package demolist;
/**
 * @Author 12629
 * @Description：
 */
public class Test {

    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(12);
        mySingleList.addLast(23);
        mySingleList.addLast(44);
        mySingleList.addLast(55);
        mySingleList.addLast(66);

        mySingleList.createLoop();

        boolean flg = mySingleList.hasCycle(mySingleList.head);

        System.out.println(flg);

        MySingleList.ListNode ret = mySingleList.detectCycle(mySingleList.head);
        System.out.println(ret.val);
    }

    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode headA,
                                                      MySingleList.ListNode headB) {

        MySingleList.ListNode newHead = new MySingleList.ListNode(-1);

        MySingleList.ListNode tmpH = newHead;

        while(headA != null && headB != null) {
            if(headA.val < headB.val) {
                tmpH.next = headA;
                tmpH = tmpH.next;
                headA = headA.next;
            }else {
                tmpH.next = headB;
                tmpH = tmpH.next;
                headB = headB.next;
            }
        }
        if(headA != null) {
            tmpH.next = headA;
        }

        if(headB != null) {
            tmpH.next = headB;
        }

        return newHead.next;
    }

    public static MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA,
                                                     MySingleList.ListNode headB) {
        //1.求2个链表的长度 假设 A链表最长
        MySingleList.ListNode pl = headA;
        MySingleList.ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        while(pl != null) {
            lenA++;
            pl = pl.next;
        }
        while(ps != null) {
            lenB++;
            ps = ps.next;
        }
        pl = headA;
        ps = headB;
        int len = lenA - lenB;
        if(len < 0) {
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //2.根据链表的长度 确定pl 指向长链表  ps指向短链表
        //3.根据第2步确定的，让pl走len步  len = Math.abs(lenA - lenB);
        while(len != 0) {
            pl = pl.next;
            len--;
        }
        //4. 一起走  直到相遇
        while(pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        if(pl == null) {
            return null;
        }
        return pl;
    }

    public static void createCut(MySingleList.ListNode headA,
                                    MySingleList.ListNode headB) {
        headB.next.next.next = headA.next.next;
    }

    public static void main3(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(12);
        mySingleList.addLast(23);
        mySingleList.addLast(44);
        mySingleList.addLast(55);
        mySingleList.addLast(66);
        mySingleList.show();

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(13);
        mySingleList2.addLast(24);
        mySingleList2.addLast(46);

        mySingleList2.addLast(56);
        mySingleList2.addLast(67);
        mySingleList2.addLast(78);
        mySingleList2.show();

        createCut(mySingleList.head,mySingleList2.head);

        MySingleList.ListNode ret = getIntersectionNode(mySingleList.head,mySingleList2.head);
        System.out.println(ret.val);
        //MySingleList.ListNode newH = mergeTwoLists(mySingleList.head,mySingleList2.head);


        //mySingleList2.show(newH);
    }

    public static void main2(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(13);
        mySingleList.addLast(4);
        mySingleList.addLast(5);
        mySingleList.addLast(6);
        mySingleList.show();
        MySingleList.ListNode ret = mySingleList.findMiddleNode2();
        System.out.println(ret.val);
       /* MySingleList.ListNode ret = mySingleList.reverseList();
        mySingleList.show(ret);*/
    }
    public static void main1(String[] args) {
        MySingleList mySingleList = new MySingleList();
        //mySingleList.createList();
        mySingleList.addLast(23);
        mySingleList.addLast(23);
        mySingleList.addLast(23);
        mySingleList.addLast(45);
        mySingleList.addLast(23);
        mySingleList.addFirst(23);
        mySingleList.show();//56 45 34 23 12
        /*//System.out.println(mySingleList.size());
        mySingleList.addIndex(6,199);
        mySingleList.show();//56 45 34 23 12*/
        /*System.out.println("=====删除====");
        mySingleList.removeAllKey(23);*/
        System.out.println("=============");
        mySingleList.clear();
        mySingleList.show();
    }
}
